﻿'use strict';

app.factory('policeData', function ($q, $http, SPHandy) {

    return {
        getForces: function ($scope) {
            var deferred = $q.defer();
            var context = SPHandy.context;
            var request = new SP.WebRequestInfo();
            request.set_url(
                "http://data.police.uk/api/forces"
                );
            request.set_method("GET");

            // We need the response formatted as JSON.
            request.set_headers({ "Accept": "application/json;odata=verbose" });
            var response = SP.WebProxy.invoke(context, request);

            context.executeQueryAsync(
                function () {
     
                    var data = [];
                        
                    if (response.get_statusCode() == 200) {

                        data = JSON.parse(response.get_body());

                        $scope.$apply(deferred.resolve(data));
                    }
                    else {
                        $scope.$apply(deferred.reject(response.get_statusCode() ));
                    }
                },
                function () {
                    $scope.$apply(deferred.reject(response.get_statusCode() ));
                }
           );

           return deferred.promise;
        },

        getStreetCrime: function ($scope) {
            //an example call, passing in preset co-ords and date
            //In another area this gives an error "403 the response is too big (or similar)" which
            //Sucks, I don't know how to alter the response size limits.
            var deferred = $q.defer();
            var context = SP.ClientContext.get_current();
            var request = new SP.WebRequestInfo();
            request.set_url(
                "http://data.police.uk/api/crimes-street/all-crime?lat=52.6143247&lng=-1.6677285&date=2013-01"
                );
            request.set_method("GET");

            // We need the response formatted as JSON.
            request.set_headers({ "Accept": "application/json;odata=verbose" });
            var response = SP.WebProxy.invoke(context, request);

            context.executeQueryAsync(
                function () {
     
                    var data = [];
                        
                    if (response.get_statusCode() == 200) {

                        data = JSON.parse(response.get_body());
                        var dataMap = $.map(data, function (item) {
                            return {
                                Category: item.category,
                                Street: item.location.street.name,
                                Lat: item.location.latitude,
                                Long: item.location.longitude
                            };
                        });

                        $scope.$apply(deferred.resolve(dataMap));
                    }
                    else {
                        $scope.$apply(deferred.reject(response.get_statusCode() ));
                    }
                },
                function () {
                    $scope.$apply(deferred.reject(response.get_statusCode() ));
                }
           );

            return deferred.promise;
        }

    };

});

